﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MediaRecommender.Security;

namespace MediaRecommender.Model
{
    public class User : IEquatable<User>
    {
        #region Fields
        private string _name;
        private string password;
        private DateTime _birthday;
        private Sex _sex;
        private MediaList _mediaList;
        private List<Genre> _preferences;
        private List<User> _friends;
        #endregion

        #region Propterties
        public string Name 
        {
            get { return this._name; }
            set { this._name = value; }
        }

        public string Password
        {
            get { return this.password; }
            private set { this.password = value; }
        }

        public int Age
        {
            get 
            {
                if (DateTime.Now.Month <= this.Birthday.Month)
                {
                    return DateTime.Now.Year - this.Birthday.Year - 1;
                }
                else if (DateTime.Now.Month == this.Birthday.Month && DateTime.Now.Day < this.Birthday.Day)
                {
                    return DateTime.Now.Year - this.Birthday.Year - 1;
                }
                else
                {
                    return DateTime.Now.Year - this.Birthday.Year;
                }
            }
        }

        public Sex Sex
        {
            get { return this._sex; }
            private set { this._sex = value; }
        }

        public DateTime Birthday
        {
            get { return this._birthday; }
            private set { this._birthday = value; }
        }

        public MediaList MediaList
        {
            get { return this._mediaList; }
        }

        public List<Genre> Preferences
        {
            get { return this._preferences; }
            set { this._preferences = value; } 
        }

        public List<User> Friends
        {
            get { return this._friends; }
        }
        #endregion

        #region Constructor
        public User(string name, string password, DateTime birthday, Sex sex, List<Genre> preferences)
        {
            Name = name;
            Password = PasswordHash.CreateHash(password);
            Birthday = birthday;
            Sex = sex;
            Preferences = preferences;
            this._friends = new List<User>();
            this._mediaList = new MediaList();
        }
        #endregion

        #region Equals
        public override int GetHashCode()
        {
            return this.Name.GetHashCode() ^
                this.Age.GetHashCode() ^
                this.Sex.GetHashCode() ^
                this.Birthday.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (this.GetType() != obj.GetType())
                return false;

            return Equals((User)obj);
        }

        public bool Equals(User obj)
        {
            if (obj == null)
                return false;
            if (ReferenceEquals(this, obj))
                return true;

            if (GetHashCode() != obj.GetHashCode())
                return false;

            return this.Name.Equals(obj.Name) &&
                this.Age.Equals(obj.Age) &&
                this.Sex.Equals(obj.Sex) &&
                this.Birthday.Equals(obj.Birthday);
        }

        public static bool operator ==(User leftUser, User rightUser)
        {
            if (ReferenceEquals(leftUser, null))
                return ReferenceEquals(rightUser, null);

            return (leftUser.Equals(rightUser));
        }

        public static bool operator !=(User leftUser, User rightUser)
        {
            return !(leftUser == rightUser);
        }
        #endregion
    }
}